package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.wjjBO.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.po.WXGoodsCategoryBO;
import com.cskaoyan.bean.vo.wjjVO.*;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.bean.vo.wjjVO.WXGoodsCategoryVO;
import com.cskaoyan.bean.vo.wjjVO.WXGoodsListVO;
import com.cskaoyan.bean.vo.wjjVO.WXHomeIndexVO;
import com.cskaoyan.service.wx.WXGoodsService;
import com.cskaoyan.service.wx.WXUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author wjj
 * @date 2022-12-31 11:02
 * @describe
 */
@RequestMapping("wx")
@RestController
public class WXUserIndexAndGoodsController {

    @Autowired
    WXUserService wxUserService;

    @Autowired
    WXGoodsService goodsService;


    /**
     * 用户个人中心
     * Request URL: http://182.92.235.201:8083/wx/user/index
     * Request Method: GET
     */
//    @RequiresPermissions("*")
    @RequestMapping("user/index")
    public BaseRespVo index() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();

        WXUserIndexBO wxUserIndexBO = new WXUserIndexBO();
        wxUserIndexBO.setId(userId);
        WXUserIndexVO userIndexVO = wxUserService.selectPersonal(wxUserIndexBO);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setData(userIndexVO);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    /**
     * Request URL: http://182.92.235.201:8083/wx/home/index
     * Request Method: GET
     * 首页展示
     */
    @RequestMapping("home/index")
    public BaseRespVo homeIndex() {
        WXHomeIndexVO homeIndexVO = wxUserService.displayHomePage();
        return BaseRespVo.ok(homeIndexVO);
    }

    /**
     * Request URL: http://182.92.235.201:8083/wx/goods/count
     * Request Method: GET
     * 共有多少个商品
     */
    @RequestMapping("goods/count")
    public BaseRespVo goodsCount() {
        Integer goodsCount = goodsService.selectGoodsCount();
        return BaseRespVo.ok(goodsCount);
    }

    /**
     * 查询某一商品种类下所有的商品 ----> 首页处点击某个商品种类跳转的url方法
     * Request URL: http://182.92.235.201:8083/wx/goods/category?id=1005000
     * Request Method: GET
     */
    @RequestMapping("goods/category")
    public BaseRespVo goodsCategory(WXGoodsCategoryBO goodsCategoryBO) {

        WXGoodsCategoryVO goodsCategoryVO = goodsService.selectGoodsByCategoryId(goodsCategoryBO);
        return BaseRespVo.ok(goodsCategoryVO);
    }


    /**
     * Request URL: http://localhost:8083/wx/goods/list?keyword=%E6%AF%8D%E4%BA%B2&page=1&limit=20&sort=name&order=desc&categoryId=0
     * Request Method: GET
     * 参数 :
     * keyword=%E6%AF%8D%E4%BA%B2
     * isNew=true
     * brandId =
     * page=1
     * limit=20
     * categoryId=0
     * sort=name
     * order=desc
     */
    @RequestMapping("goods/list")
    public BaseRespVo goodsList(WXGoodsListBo goodsListBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        Integer userId = null;
        if (principal != null){
            userId = principal.getId();
        }
        goodsListBo.setUserId(userId);
        WXGoodsListVO goodsListVO = goodsService.selectGoodsList(goodsListBo);
        return BaseRespVo.ok(goodsListVO);
    }

    /**
     * Request URL: http://localhost:8083/wx/goods/detail?id=1023003
     * Request Method: GET
     */
    @RequestMapping("goods/detail")
    public BaseRespVo goodsDetail(WXGoodsDetailBO goodsDetailBO) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipal();
        if (primaryPrincipal != null){
            Integer userId = primaryPrincipal.getId();
            goodsDetailBO.setUserId(userId);
        }
        WXGoodsDetailVO goodsDetailVO = goodsService.selectGoodsDetail(goodsDetailBO);
        return BaseRespVo.ok(goodsDetailVO);
    }

    /**
     * Request URL: http://localhost:8083/wx/goods/related?id=1023003
     * Request Method: GET
     */
    @RequestMapping("goods/related")
    public BaseRespVo goodsRelated(WXGoodsRelatedBO goodsRelatedBO) {
        WXGoodsRelatedVO goodsRelatedVO = goodsService.selectGoodsRelatedById(goodsRelatedBO);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(goodsRelatedVO);
        return baseRespVo;
    }

}
